package com.newcapec.news.servlet;

import com.newcapec.news.entity.Channel;
import com.newcapec.news.entity.News;
import com.newcapec.news.service.ChannelService;
import com.newcapec.news.service.NewService;
import com.newcapec.news.service.impl.ChannelServiceImpl;
import com.newcapec.news.service.impl.NewServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description: 登录页面的展示
 * @Author: fujun
 * @Date : 2022/3/16 16:08
 * @Version : 1.0
 * @company 新开普电子股份有限公司
 */
@WebServlet("/html/main.html")
public class ShowMainHtmlServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //查询所有的栏目

        ChannelService channelService=new ChannelServiceImpl();
        List<Channel> channels =channelService.queryChannel();


          //获取一个默认的 栏目id

        Channel channel = channels.get(0);
        int channel_id=channel.getId();
        //根据栏目的id来查询指定的新闻信息
        String channelId = req.getParameter("channelId");
        if(channelId!=null){
            channel_id=Integer.parseInt(channelId);
        }

        NewService newService=new NewServiceImpl();

        List<News> news = newService.queryNewsByChannel(channel_id);


        req.setAttribute("channels",channels);
        req.setAttribute("news",news);
        req.getRequestDispatcher("main.jsp").forward(req,resp);

        System.out.println(channels);
        System.out.println(news);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
